package com.adee.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Test029_URL {
    public static void main(String[] args) {
        // test1();
        test2();
    }

    // 读取网络资源
    public static void test1() {
        BufferedReader buffReader = null;
        try {
            /*
            *   协议：http
                主机：www.opplestore.com
                端口：-1
                文件名：/item.html?itemId=1150041800000272
                路径：/item.html
                锚：body
                查询字符串：itemId=1150041800000272
            * */
            URL url = new URL("https://blog.csdn.net/weixin_36143290/article/details/114211477?itemId=1150041800000272#body");
            System.out.println("协议：" + url.getProtocol());
            System.out.println("主机：" + url.getHost());
            System.out.println("端口：" + url.getPort());
            System.out.println("文件名：" + url.getFile());
            System.out.println("路径：" + url.getPath());
            System.out.println("锚：" + url.getRef());
            System.out.println("查询字符串：" + url.getQuery());
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(is);
            buffReader = new BufferedReader(reader);
            byte[] buff = new byte[1024];
            int len = buff.length;
            int size;
            StringBuffer sb = new StringBuffer();
            System.out.println("读取数据：");
//            while ((size = is.read(buff, 0, len)) >= 0) {
//                System.out.print(new String(buff, 0, size, StandardCharsets.UTF_8));
//            }

            String s;
            while((s = buffReader.readLine()) != null) {
                System.out.println(s);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(buffReader != null) {
                try {
                    buffReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 读取本地文件资源
    public static void test2() {
        try {
            // URL url2 = new URL("file:Test029_URL.class");
            URL url2 = new URL("file:\\F:\\mycode\\tomcat\\apache-tomcat-8.5.81-src\\.gitignore");
            System.out.println("协议：" + url2.getProtocol());
            System.out.println("主机：" + url2.getHost());
            System.out.println("端口：" + url2.getPort());
            System.out.println("文件名：" + url2.getFile());
            System.out.println("路径：" + url2.getPath());
            System.out.println("锚：" + url2.getRef());
            System.out.println("查询字符串：" + url2.getQuery());
            InputStream is2 = url2.openConnection().getInputStream();
            BufferedReader buffReader2 = new BufferedReader(new InputStreamReader(is2));
            String s2;
            while((s2 = buffReader2.readLine()) != null) {
                System.out.println(s2);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
